package com.beeweeb.services.tps.concurrent;

import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author guido d'albore
 */
public class RunnableMethod implements Runnable {
    Logger logger = LoggerFactory.getLogger("ThreadPoolScheduler");
    
    Object instance;
    Method method;
    Object parameter;
    
    public RunnableMethod(Class clazz, Method method) throws Exception {
        this(clazz, method, null);
    }

    public RunnableMethod(Class clazz, Method method, Object parameter) throws Exception {
        instance = clazz.newInstance();
        this.parameter = parameter;
        this.method = method;
    }

    public void run() {
        //logger.info("run() method called");
        
        try {
            if(parameter != null) {
                method.invoke(instance, parameter);
            } else {
                method.invoke(instance);
            }
        } catch(Exception ex) {
            logger.error("Found errors on run() method. ", ex);
        }
    }

    @Override
    public String toString() {
        return "RunnableMethod{" + "instance=" + instance + "method=" + method + "parameter=" + parameter + '}';
    }
}
